package cn.sumpu.app.coolsk.dao.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.coolsk.dao.OrderDao;
import cn.sumpu.app.coolsk.domain.AdminUser;
import cn.sumpu.app.coolsk.domain.Order;
import cn.sumpu.app.coolsk.domain.OrderLog;
import cn.sumpu.app.coolsk.domain.OrderProduct;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class OrderDaoImpl extends SqlSessionDaoSupport implements OrderDao {

	private static Logger logger = LoggerFactory.getLogger(OrderDaoImpl.class);

	@Override
	public void delOrderProduct(Map<String, Object> map)
			throws AppBusinessException {
		try {
			   getSqlSession().delete("cn.sumpu.app.coolsk.persistence.OrderMapper.delOrderProduct",map);
			} catch (Exception e) {
				if (logger.isErrorEnabled()) {
					logger.error("delete database failure.", e);
				}
				throw new AppBusinessException("delete database failure.", e);
			}
	}

	@Override
	public int getOrderCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.coolsk.persistence.OrderMapper.getOrderCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Order> getOrder(Map<String, Object> map)
			throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.OrderMapper.getOrder",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderProduct> getOrderProduct(Map<String, Object> map)
			throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.OrderMapper.getOrderProduct",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public void insertOrder(Order order) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.OrderMapper.insertOrder",order);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insert database failure.", e);
			}
			throw new AppBusinessException("insert database failure.", e);
		}
	}

	@Override
	public void insertOrderProduct(OrderProduct op) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.OrderMapper.insertOrderProduct",op);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insert database failure.", e);
			}
			throw new AppBusinessException("insert database failure.", e);
		}
	}

	@Override
	public void updateOrder(Order order) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.OrderMapper.updateOrder",order);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}

	/**
	 * 查询我家的订单
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Order> getOrderListByMemberId(Map<String, Object> map) throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.OrderMapper.getOrderListByMemberId",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrderListByMemberId database failure.", e);
			}
			throw new AppBusinessException("getOrderListByMemberId database failure.", e);
		}
	}
	/**
	 * 产询订单
	 */
	@Override
	public Order getOrderDtlById(Map<String, Object> map) throws AppBusinessException{
		
		try {
			return (Order) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.OrderMapper.getOrderDtlById",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrderDtlById failure.", e);
			}
			throw new AppBusinessException("getOrderDtlById failure.", e);
		}
	}
	/**
	 * 
	 */
	@Override  
	public void insertOrderLog(OrderLog orderLog) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.OrderMapper.insertOrderLog",orderLog);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertOrderLog database failure.", e);
			}
			throw new AppBusinessException("insertOrderLog database failure.", e);
		}
	}
	/**
	 * 查询订单最近的那条记录
	 * 
	 */
	
	@Override  
	public OrderLog selectOrderLogByid(Map<String, Object> map) throws AppBusinessException {
		try {
			return (OrderLog) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.OrderMapper.selectOrderLogByid",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectOrderLogByid database failure.", e);
			}
			throw new AppBusinessException("selectOrderLogByid database failure.", e);
		}
	}
	/**
	 * 日志总数 订单
	 */
	@Override
	public int getOrderLogCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.coolsk.persistence.OrderMapper.getOrderLogCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrderListCount database failure.", e);
			}
			throw new AppBusinessException("getOrderListCount database failure.", e);
		}
	}
	//
	@SuppressWarnings("unchecked")
	@Override
	public List<OrderLog> getOrderLogList(Map<String, Object> map) throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.OrderMapper.getOrderLogList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrderLogList database failure.", e);
			}
			throw new AppBusinessException("getOrderLogList database failure.", e);
		}
	}
	@Override
	public void updateOrderLock(Order order) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.OrderMapper.updateOrderLock",order);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}
}
